﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Dexter.DataTransferObject;
using Dexter.Dependency;
using Dexter.Services;

namespace Dexter.Web.Mvc.Helpers {
	public static class BackOfficeHelper {
		public static MvcHtmlString CategoriesStructure ( this HtmlHelper helper, IEnumerable<CategoryDto> categories, string bindePrefix, int[] selectedValues ) {
			StringBuilder sb = new StringBuilder ( );

			int categoryNumber = 0;

			CreateUnorderedList ( categories , sb , ref categoryNumber , bindePrefix , selectedValues );

			return MvcHtmlString.Create(sb.ToString ( ));
		}

		static void CreateUnorderedList ( IEnumerable <CategoryDto> categories , StringBuilder sb , ref int categoryNumber , string bindePrefix , IEnumerable <int> selectedValues ) {
			if ( categories == null || !categories.Any ( ) ) {
				return;
			}

			sb.Append ( "<ul class=\"collapsible-list with-bg\">" );

			foreach ( var category in categories ) {
				sb.Append ( "<li class=\"closed\">" );

				sb.Append ( category.Categories.Any ( )
				            	? "<b class=\"toggle\"></b>"
				            	: "<b></b>" );

				sb.Append ( "<span>" );
				sb.AppendFormat ( "<input type=\"hidden\" name=\"{2}.Categories[{0}].ID\" value=\"{1}\" />" , categoryNumber , category.Id , bindePrefix );
				sb.AppendFormat ( "<input type=\"checkbox\" name=\"{2}.Categories[{0}].Name\" value=\"{1}\" {3} /> " , categoryNumber , category.Name , bindePrefix , selectedValues != null && selectedValues.Contains ( category.Id )
				                                                                                                                                                      	? "checked=\"checked\""
				                                                                                                                                                      	: string.Empty );
				sb.Append ( category.Name );
				sb.Append ( "</span>" );

				categoryNumber++;

				CreateUnorderedList ( category.Categories , sb , ref categoryNumber , bindePrefix , selectedValues );
			}

			sb.Append ( "</ul>" );
		}

		public static MvcHtmlString CreateMenu ( this HtmlHelper helper ) {
			IUrlBuilder u = DexterContainer.Resolve <IUrlBuilder> ( );

			StringBuilder sb = new StringBuilder ( );

			sb.Append ( "<ul class=\"container_12\">" );

			AddHome ( sb, u, helper.ViewContext );
			AddPost ( sb, u, helper.ViewContext );
			//AddComments ( sb, u, helper.ViewContext );
			AddStats ( sb, u, helper.ViewContext );
			AddSettigns ( sb, u, helper.ViewContext );
			Addbackup ( sb, u, helper.ViewContext );
			AddUser ( sb, u, helper.ViewContext );

			sb.Append ( "</li>" );
			sb.Append ( "</ul>" );

			return MvcHtmlString.Create ( sb.ToString() );
		}

		static void AddHome ( StringBuilder sb ,  IUrlBuilder urlBuilder, ViewContext viewContext ) {
			bool isCurrent = RouteHelper.IsCurrentController ( "Admin", "Home", viewContext );

			sb.AppendFormat ( "<li class=\"home {0}\">", isCurrent ? "current" : string.Empty );
			sb.AppendFormat ( "<a href=\"{0}\" title=\"Home\">Home</a></li>", urlBuilder.BackOffice.Home );
		}

		static void Addbackup ( StringBuilder sb , IUrlBuilder urlBuilder , ViewContext viewContext ) {
			bool isCurrent = RouteHelper.IsCurrentController ( "Admin", "Backup", viewContext );
			
			sb.AppendFormat ( "<li class=\"backup {0}\">", isCurrent ? "current" : string.Empty );
			sb.AppendFormat ( "<a href=\"{0}\" title=\"Backup\">Backup</a>", urlBuilder.BackOffice.Home );
			sb.Append ( "<ul>" );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Comments.\">Comments</a></li>", urlBuilder.BackOffice.CommentsExport);
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Comments.\">Import Posts</a></li>", urlBuilder.BackOffice.ImportPosts);
			sb.Append ( "</ul>" );
			sb.Append ( "</li>" );
		}

		static void AddSettigns ( StringBuilder sb , IUrlBuilder urlBuilder , ViewContext viewContext ) {
			bool isCurrent = RouteHelper.IsCurrentController ( "Admin", "Settings", viewContext );

			sb.AppendFormat ( "<li class=\"settings {0}\">", isCurrent ? "current" : string.Empty );
			sb.AppendFormat ( "<a href=\"{0}\" title=\"settings\">settings</a>", urlBuilder.BackOffice.SiteConfiguration );
			sb.Append ( "<ul>" );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Site Configuration.\">Site Configuration</a></li>", urlBuilder.BackOffice.SiteConfiguration );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Rebuild Search Index.\">Rebuild Search Index</a></li>", urlBuilder.BackOffice.RebuildSearchIndex );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Plugins.\">Plugin settings</a></li>", urlBuilder.BackOffice.PluginSettings );
			sb.Append ( "</ul>" );
			sb.Append ( "</li>" );
		}

		static void AddStats ( StringBuilder sb , IUrlBuilder urlBuilder , ViewContext viewContext ) {
			bool isCurrent = RouteHelper.IsCurrentController ( "Admin", "Statistics", viewContext );

			sb.AppendFormat ( "<li class=\"stats {0}\">", isCurrent ? "current" : string.Empty );
			sb.AppendFormat ( "<a href=\"{0}\" title=\"Stats\">Stats</a>", urlBuilder.BackOffice.Referrers );
			sb.Append ( "<ul>" );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Referrers\">Referrers</a></li>", urlBuilder.BackOffice.Referrers );
			sb.Append ( "</ul>" );
			sb.Append ( "</li>" );
		}

		static void AddComments ( StringBuilder sb , IUrlBuilder urlBuilder , ViewContext viewContext ) {
			sb.Append ( "<li class=\"comments\">" );
			sb.AppendFormat ( "<a href=\"{0}\" title=\"Comments\">Comments</a>", urlBuilder.BackOffice.Home );
			sb.Append ( "<ul>" );
			sb.Append ( "</ul>" );
			sb.Append ( "</li>" );
		}

		static void AddUser ( StringBuilder sb , IUrlBuilder urlBuilder , ViewContext viewContext ) {
			bool isCurrent = RouteHelper.IsCurrentController ( "Admin", "Account", viewContext );

			sb.AppendFormat ( "<li class=\"users {0}\">", isCurrent ? "current" : string.Empty );
			sb.AppendFormat ( "<a href=\"{0}\" title=\"Users\">Users</a>", urlBuilder.BackOffice.UserList );
			sb.Append ( "<ul>" );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"All user.\">Users</a></li>", urlBuilder.BackOffice.UserList );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Create new user.\">New</a></li>", urlBuilder.User.NewUser );
			sb.Append ( "</ul>" );
			sb.Append ( "</li>" );
		}

		static void AddPost ( StringBuilder sb , IUrlBuilder urlBuilder , ViewContext viewContext ) {
			bool isCurrent = RouteHelper.IsCurrentController ( "Admin" , "Post" , viewContext ) || RouteHelper.IsCurrentController ( "Admin" , "Category" , viewContext ) || RouteHelper.IsCurrentController ( "Admin" , "Page" , viewContext );

			sb.AppendFormat ( "<li class=\"write {0}\">", isCurrent ? "current" : string.Empty );
			sb.AppendFormat ( "<a href=\"{0}\" title=\"Post & Pages\">Posts</a>", urlBuilder.BackOffice.NewPost );
			sb.Append ( "<ul>" );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"All Posts.\">Posts</a></li>", urlBuilder.BackOffice.PostList );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Add new post.\">Add Post</a></li>", urlBuilder.BackOffice.NewPost );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"All pages.\">Pages</a></li>", urlBuilder.BackOffice.PageList );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Add new page.\">Add Page</a></li>", urlBuilder.BackOffice.NewPage );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"All categories.\">Categories</a></li>", urlBuilder.BackOffice.CategoryList );
			sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Add new category.\">Add Category</a></li>", urlBuilder.BackOffice.NewCategory );
			sb.Append ( "</ul>" );
			sb.Append ( "</li>" );
		}

		public static MvcHtmlString RenderBackOffice ( this HtmlHelper helper, int pageSize, int pageIndex, long totalCounts, string baseUrl ) {

			string prevImageString = string.Format ( "<img src=\"{0}\" width=\"16\" height=\"16\"> Prev", VirtualPathUtility.ToAbsolute ( "~/admin/Resources/images/Icons/Fugue/navigation-180.png" ) );
			string nextImageString = string.Format ( "Next <img src=\"{0}\" width=\"16\" height=\"16\">", VirtualPathUtility.ToAbsolute ( "~/admin/Resources/images/Icons/Fugue/navigation.png" ) );

			return Render ( helper , pageSize ,
			                                       pageIndex ,
			                                       totalCounts ,
			                                       "controls-buttons" ,
			                                       true ,
												   prevImageString,
												   nextImageString,
			                                       "current" ,
			                                       baseUrl ) ;
		}

		public static MvcHtmlString Render ( this HtmlHelper helper, int pageSize, int pageIndex, long totalCounts, string ulClass, bool renderPreviousAndNext, string previousText, string nextText, string currentClass, string baseUrl ) {
			IUrlBuilder u = DexterContainer.Resolve<IUrlBuilder> ( );

			StringBuilder sb = new StringBuilder ( );

			if (ulClass.IsNullOrEmpty ( ))
				sb.Append ( "<ul>" );
			else
				sb.AppendFormat ( "<ul class=\"{0}\">", ulClass );

			if (pageIndex > 1 && renderPreviousAndNext)
				sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Previous\">{1}</a></li>", u.SetPageToUrl ( pageIndex - 1, helper.ViewContext), previousText );

			for (int page = pageIndex; page < Math.Round ( ( totalCounts / pageSize ) + 0.5 ) && page < pageIndex + pageSize; page++) {
				sb.AppendFormat ( page == pageIndex
									? "<li><a href=\"{0}\" title=\"Page {1}\"><b>{1}</b></a></li>"
									: "<li><a href=\"{0}\" title=\"Page {1}\" class=\"current\"><b>{1}</b></a></li>", u.SetPageToUrl ( page + 1, helper.ViewContext ), page + 1 );
			}

			if (( pageIndex * pageSize ) <= totalCounts)
				sb.AppendFormat ( "<li><a href=\"{0}\" title=\"Next\">{1}</a></li>", u.SetPageToUrl ( pageIndex + 1, helper.ViewContext ), nextText );

			sb.Append ( "<ul>" );

			return MvcHtmlString.Create ( sb.ToString ( ) );
		}
	}
}